import shutil
from pathlib import Path


def delete_project_structure(device_type):
    """安全删除项目结构（带多重确认机制）"""
    base_path = Path(__file__).parent.parent.parent

    # 定义所有可能的目标路径
    target_paths = {
        "元素目录": base_path / "base" / f"elements_{device_type}",
        "配置文件目录": base_path / "config" / f"config_{device_type}",
        "页面包目录": base_path / "pages" / f"pages_{device_type}",
        "测试用例包目录": base_path / "test_cases" / f"test_cases_{device_type}"
    }

    # 显示待删除内容
    print("即将删除以下内容：")
    for name, path in target_paths.items():
        if path.exists():
            print(f"  [{name}] => {path.resolve()}")

    # 双重确认机制
    confirm = input(f"\n确认要删除 {device_type} 的所有相关文件吗？(y/n): ").lower()
    if confirm != 'y':
        print("取消删除操作")
        return

    final_confirm = input(f"输入 'delete {device_type}' 确认彻底删除: ")
    if final_confirm != f"delete {device_type}":
        print("确认短语不匹配，操作已取消")
        return

    # 执行删除操作
    try:
        for path in target_paths.values():
            if path.exists():
                if path.is_dir():
                    shutil.rmtree(path)
                else:
                    path.unlink()
        print(f"\n成功删除 {device_type} 相关文件！")

    except PermissionError as e:
        print(f"\n权限不足：{e.filename}\n请关闭正在使用的文件后重试")
    except Exception as e:
        print(f"\n删除过程中出现错误：{str(e)}")


if __name__ == "__main__":
    device = input("请输入要清理的设备类型名称: ").strip()

    if device:
        delete_project_structure(device)
    else:
        print("错误：设备类型不能为空！")